package vip.liux.backend.infrastructure.jpa.pay;

import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.transaction.annotation.Transactional;
import vip.liux.contracts.models.pay.PayWalletRecharge;
import vip.liux.contracts.repositories.pay.PayWalletRechargeRepository;
import vip.liux.contracts.shared.enums.pay.PayRefundStatus;

import java.math.BigDecimal;
import java.time.LocalDateTime;

public interface PayWalletRechargeJpaRepository extends JpaRepository<PayWalletRecharge, Long>, PayWalletRechargeRepository, JpaSpecificationExecutor<PayWalletRecharge> {

    @Modifying
    @Override
    @Query("UPDATE PayWalletRecharge p SET p.payStatus = :payStatus, p.payTime = :payTime, p.payChannelCode = :payChannelCode WHERE p.id = :id AND p.payStatus = :wherePayStatus")
    int updateByIdAndPaid(Long id, boolean wherePayStatus, boolean payStatus, LocalDateTime payTime, String payChannelCode);

    @Transactional
    @Modifying
    @Query("update PayWalletRecharge p set p.refundStatus = :refundStatus, p.payRefundNo = :payRefundNo where p.id = :id")
    int updateRefundStatusAndPayRefundNoById(PayRefundStatus refundStatus, String payRefundNo, Long id);

    /**
     * 根据ID和当前退款状态更新为退款失败状态
     *
     * @param id                钱包充值记录ID
     * @param whereRefundStatus 预期的当前退款状态（条件判断）
     * @param payRefundStatus   要更新的新退款状态（应为失败状态）
     */
    @Transactional
    @Modifying
    @Query("UPDATE PayWalletRecharge p SET p.refundStatus = :payRefundStatus WHERE p.id = :id AND p.refundStatus = :whereRefundStatus")
    void updateByIdAndRefundedFailure(@Param("id") Long id,
                                      @Param("whereRefundStatus") PayRefundStatus whereRefundStatus,
                                      @Param("payRefundStatus") PayRefundStatus payRefundStatus);

    /**
     * 根据ID和当前退款状态更新为退款成功状态，并同步相关金额及成功时间
     *
     * @param id                钱包充值记录ID
     * @param whereRefundStatus 预期的当前退款状态（条件判断）
     * @param payRefundStatus   要更新的新退款状态（应为成功状态）
     * @param refundTime        退款成功时间
     * @param totalPrice        总金额
     * @param payPrice          实付金额
     * @param bonusPrice        赠送金额
     */
    @Transactional
    @Modifying
    @Query("UPDATE PayWalletRecharge p " +
            "SET p.refundStatus = :payRefundStatus, " +
            "    p.refundTime = :refundTime, " +
            "    p.totalPrice = :totalPrice, " +
            "    p.payPrice = :payPrice, " +
            "    p.bonusPrice = :bonusPrice " +
            "WHERE p.id = :id AND p.refundStatus = :whereRefundStatus")
    void updateByIdAndRefundedSuccess(@Param("id") Long id,
                                      @Param("whereRefundStatus") PayRefundStatus whereRefundStatus,
                                      @Param("payRefundStatus") PayRefundStatus payRefundStatus,
                                      @Param("refundTime") LocalDateTime refundTime,
                                      @Param("totalPrice") BigDecimal totalPrice,
                                      @Param("payPrice") BigDecimal payPrice,
                                      @Param("bonusPrice") BigDecimal bonusPrice);

}